package network.TCP;

import java.io.*;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA
 * Description:客户端
 * User:22954
 * Date:2024-04-03
 * Time:16:53
 */
public class Client {
    public static void main(String[] args) throws IOException {
//        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        Socket socket = new Socket("10.122.229.191",9999);//Socket对象连接host(ip) + port(端口)
        System.out.println("linked server socket : " + socket.getClass());
        OutputStream outputStream = socket.getOutputStream();
//        outputStream.write("hello server".getBytes());
//        socket.shutdownOutput();//设置写入结束标记
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
        bw.write("hello server");
        bw.flush();//输入输出流的关闭会导致socket关闭,遇到有缓存流的情况只能用flush刷新缓存区将数据从流中输出
        socket.shutdownOutput();//设置写入结束标记
        InputStream inputStream = socket.getInputStream();
//        byte[] buf = new byte[1024];
//        int readLen = 0;
//        while ((readLen = inputStream.read(buf)) != -1) {
//            System.out.println(new String(buf, 0, readLen));
//        }
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String data;
        while ((data = br.readLine()) != null) {
            System.out.println(data);
        }
//        outputStream.close();
//        inputStream.close();
        br.close();
        socket.close();
        System.out.println("broken linked");
    }
}
